/*
** check_cmd.c for check_cmd in /home/yang_j/rendu/CORE_war
** 
** Made by yang jie
** Login   <yang_j@epitech.net>
** 
** Started on  Mon Dec  9 13:51:23 2013 yang jie
** Last update Mon Dec  9 16:14:18 2013 yang jie
*/
# include"op.h"
typedef struct	p
{
  int		p1;
  int		p2;
  int		p3;
}		t_p

op_t    op_tab[] =
  {
    {"live", 1, {T_DIR}, 1, 10, "alive"},
    {"ld", 2, {T_DIR | T_IND, T_REG}, 2, 5, "load"},
    {"st", 2, {T_REG, T_IND | T_REG}, 3, 5, "store"},
    {"add", 3, {T_REG, T_REG, T_REG}, 4, 10, "addition"},
    {"sub", 3, {T_REG, T_REG, T_REG}, 5, 10, "soustraction"},
    {"and", 3, {T_REG | T_DIR | T_IND, T_REG | T_IND | T_DIR, T_REG}, 6, 6,
     "et (and  r1, r2, r3   r1&r2 -> r3"},
    {"or", 3, {T_REG | T_IND | T_DIR, T_REG | T_IND | T_DIR, T_REG}, 7, 6,
     "ou  (or   r1, r2, r3   r1 | r2 -> r3"},
    {"xor", 3, {T_REG | T_IND | T_DIR, T_REG | T_IND | T_DIR, T_REG}, 8, 6,
     "ou (xor  r1, r2, r3   r1^r2 -> r3"},
    {"zjmp", 1, {T_DIR}, 9, 20, "jump if zero"},
    {"ldi", 3, {T_REG | T_DIR | T_IND, T_DIR | T_REG, T_REG}, 10, 25,
     "load index"},
    {"sti", 3, {T_REG, T_REG | T_DIR | T_IND, T_DIR | T_REG}, 11, 25,
     "store index"},
    {"fork", 1, {T_DIR}, 12, 800, "fork"},
    {"lld", 2, {T_DIR | T_IND, T_REG}, 13, 10, "long load"},
    {"lldi", 3, {T_REG | T_DIR | T_IND, T_DIR | T_REG, T_REG}, 14, 50,
     "long load index"},
    {"lfork", 1, {T_DIR}, 15, 1000, "long fork"},
    {"aff", 1, {T_REG}, 16, 2, "aff"},
    {0, 0, {0}, 0, 0, 0}
  };

int	para_nbr(char **cmd)
{
  int	i;

  i = 0;
  while (cmd[i])
    i++;
  return (i);
}

t_p	*para_type(char **cmd, int p_nbr)
{
  t_p	*para = malloc(sizeof(t_p));

  if (p_nbr == 1)
    para->p1 = check_type(cmd[0]);
  else if (size == 2)
    para->p1 = check_type(cmd[1]);
  else if (p_nbr == 3)
    {
      para->p1 = check_type(cmd[1]);
      para->p2 = check_type(cmd[2]);
    }
   else if (p_nbr == 4)
    {
      para->p1 = check_type(cmd[1]);
      para->p2 = check_type(cmd[2]);
      para->p3 = check_type(cmd[1]);
    }
  return (para);
}

int	check_cmd(char **cmd)
{
  int	i;
  int	p_nbr;
  int	p_type;

  p_nbr = para_nbr(cmd);
  p_type = para_type(cmd, p_nbr);
  i = 0;
  while (i < 17)
    {
      if (my_strcmp(cmd[0],op_tab[i].mnemonique) == 0)
	{
	  if (p_nbr > 1)
	    p_nbr = p_nbr - 1;
	  else if (op_tab[i].nbr_args == (48 + p_nbr))
	    {
	      if ((p_type & op_tab[i].type) == op_tab[i].type)
		return (op_tab[i].code);
	      else
		my_putstr("para_type is wrong.");
	    }
	  else
	    my_putstr("parametter number is wrong.");
	}
      i++;
    }
  return (0);
}

int	main(int ac, char **av)
{
  int res;

  res = check_cmd(av);
  my_put_nbr(res);
}
